package com.cgsrp.cgsrp_shopping.service.impl;

import com.cgsrp.cgsrp_shopping.dao.ProductMapper;
import com.cgsrp.cgsrp_shopping.domain.Product;
import com.cgsrp.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.cgsrp.cgsrp_shopping.domain.ShoppingCart;
import com.cgsrp.cgsrp_shopping.dao.ShoppingCartMapper;
import com.cgsrp.cgsrp_shopping.service.ShoppingCartService;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService{

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private ProductMapper productMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return shoppingCartMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ShoppingCart record) {
        return shoppingCartMapper.insert(record);
    }

    @Override
    public int insertSelective(ShoppingCart record) {
        return shoppingCartMapper.insertSelective(record);
    }

    @Override
    public ShoppingCart selectByPrimaryKey(Integer id) {
        return shoppingCartMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ShoppingCart record) {
        return shoppingCartMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ShoppingCart record) {
        return shoppingCartMapper.updateByPrimaryKey(record);
    }

    @Override
    public Result add(Integer userId, Integer productId, Integer number) {
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product.getNumber()<number){
            return Result.failure("库存不足，无法添加");
        }
        List<ShoppingCart> oldCart1 = shoppingCartMapper.findByProductIdUserId(userId,productId);
        if (!oldCart1.isEmpty()){
            return Result.failure("您的购物车中已有这件商品");
        }else {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(userId);
            shoppingCart.setProductId(productId);
            shoppingCart.setNumber(number);
            BigDecimal bigDecimal = new BigDecimal(number);
            shoppingCart.setCost(product.getPrice().multiply(bigDecimal));
            shoppingCart.setState(0);
            return shoppingCartMapper.insert(shoppingCart)>0?Result.success("添加购物车成功"):Result.failure("添加购物车失败");
        }
    }

    @Override
    public Result list(Integer curPage, Integer pageSize, Integer userId) {
        List<ShoppingCart> list = shoppingCartMapper.list(userId);
        List<ShoppingCart> result;
        if (pageSize<list.size()-(curPage-1)*pageSize){
            result= list.subList((curPage - 1) * pageSize, (curPage - 1) * pageSize + pageSize);
        }else {
            result=list.subList((curPage - 1) * pageSize, list.size());
        }
        PageInfo<ShoppingCart> pageInfo = new PageInfo<>(result);
        pageInfo.setTotal(list.size());
        PageHelper.startPage(curPage, pageSize);
        return Result.suc("查询成功",pageInfo);
    }

}
